# coding=utf-8
"""
作者：董新强 
创建时间：2020年3月25日
描述：将raw_questions转换为cat_questions
"""
import numpy

from catsim import irt
from db_models.cat_questions import CatQuestions
from db_models.raw_questions import RawQuestions
from framework.db_session import DbSession


def raw_to_std(aid, dim):
    titles = set()
    with DbSession.create() as db:
        # ablities_dic = {name: id for id, name in db.query(CatAbilities.id, CatAbilities.name)}
        query = db.query(RawQuestions.title, RawQuestions.options, RawQuestions.dim, RawQuestions.b, RawQuestions.answer) \
            .filter(RawQuestions.dim == dim) \
            .order_by(RawQuestions.dim).all()
        for title, options, dim, b, answer in query:
            if title in titles:  # 题干去重
                print(titles, options, answer)
                continue
            titles.add(title)

            q = CatQuestions()
            q.a = 0.3
            q.b = b
            q.c = 0.25
            q.d = 1
            q.answer = answer
            info = irt.max_info_hpc(numpy.array([[q.a, q.b, q.c, q.d]]))
            q.info = float(info[0])
            q.aid = aid
            q.type = 'MC4'
            q.exp_rate = 0.5

            q.title = title
            q.options = options

            db.add(q)


if __name__ == '__main__':
    raw_to_std(8, dim='psyc2')
